package com.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.app.mapper.IntegralRuleLogMapper;
import com.app.mapper.IntegralRuleMapper;
import com.app.model.IntegralRule;
import com.app.model.IntegralRuleLog;
import com.app.model.InviteConfigLogModel;
import com.app.model.VoucherRecordModel;
import com.app.service.IIntegralRuleLogService;
import com.app.service.IIntegralRuleService;
import com.app.vo.IntegralRuleLogVo;
import com.app.vo.UserVVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户积分表 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2023-12-09
 */
@Service
public class IntegralRuleLogServiceImpl extends ServiceImpl<IntegralRuleLogMapper, IntegralRuleLog> implements IIntegralRuleLogService {

    @Autowired
    private IntegralRuleLogMapper integralRuleLogMapper;

    @Override
    public IntegralRuleLogVo getIntegralRuleLogList(Integer page, Integer limit,String integralRuleId){

        IntegralRuleLogVo vo = new IntegralRuleLogVo();
        Page<IntegralRuleLog> page1 = new Page<>(page, limit);

        QueryWrapper<IntegralRuleLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("integral_rule_id", integralRuleId);
        queryWrapper.orderByDesc("create_time");

        // 使用 selectPage 方法进行分页查询
        Page<IntegralRuleLog> voucherRecordModelPage = integralRuleLogMapper.selectPage(page1, queryWrapper);
        List<IntegralRuleLog> list = voucherRecordModelPage.getRecords(); // 获取分页后的记录列表
        // 接下来，您可以像之前一样处理每条记录，例如解析 JSON 字符串等
        for (IntegralRuleLog model : list){
            String oldJsonstr = model.getOldJsonstr();
            String newJsonstr = model.getNewJsonstr();
            JSONObject oldJSON = JSONObject.parseObject(oldJsonstr);
            JSONObject newJSON = JSONObject.parseObject(newJsonstr);
            model.setOldJson(oldJSON);
            model.setNewJson(newJSON);
        }
        vo.setList(voucherRecordModelPage.getRecords());
        vo.setCount(voucherRecordModelPage.getTotal());
        return vo;
    };

}
